﻿<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link type="text/css" href="../Skin/Plane/css/List.css?201810292" rel="stylesheet" />
    <link type="text/css" href="../Layui/css/layui.css" rel="stylesheet" media="all">
    <script type="text/javascript" src="../Htc/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="../Layui/layui.js"></script>
    <style>
        .layui-laypage .layui-laypage-curr .layui-laypage-em {
            background-color: #1E9FFF;
        }

        .layui-text em, .layui-word-aux {
            color: #fff !important;
            padding: 0 0px !important
        }
    </style>
</head>
<body>
    <form id="form1" class="layui-form">
        <div class="poppanButton">
            @if (ViewBag.Error != "")
            {
                <font>错误信息：<label class="layui-badge">@ViewBag.Error</label></font>
            }
            <table style="width:100%">
                <tr>
                    <td align="left">待分配数量：<label  id="Wfpsl">@ViewBag.TotalDis</label></td>
                    <td >已分配数量：<label id="Yfpsl">@ViewBag.TotalRece</label></td>
                    <td align="right">
                        <div class="poppanButton">
                            <button class="layui-btn layui-btn-normal" onclick="Avg();return false;">平均分配</button>
                            <button class="layui-btn layui-btn-normal" onclick="Finish();return false;">确定</button>

                            <button class="layui-btn layui-btn-primary" onclick="parent.HideModal('popEdit');return false;">关闭</button>
                        </div>
                    </td>
                </tr>
            </table>
           

            <table class="layui-table" lay-filter="dgrid" id="dgrid">
                <thead>
                    <tr>
                        <th lay-data="{field:'id',type: 'checkbox'}"></th>
                        <th lay-data="{field:'sshowname',sort: true}">顾问</th>
                        <th lay-data="{field:'agentcount', width:200, sort: true, edit: 'text'}">分配数量</th>
                    </tr>
                </thead>
            </table>
        </div>
      
    </form>

    <script>        
        layui.use(['element', 'table', 'layer', 'laypage', 'form', 'laydate', 'upload'], function () {
            var table = layui.table;
            table.init("dgrid", {                
                height:'full-120',               
                url:"../Customer/Distribution/UserAdviser"
            });
           
            table.on('checkbox(dgrid)', function (obj) {              
                //var data = table.cache.dgrid;    
                obj.data["agentcount"].edit = 'text';
            });
            table.on('row(dgrid)', function (obj) {
                var t = false;
                var checkStatus = layui.table.checkStatus('dgrid');
                if (checkStatus.data.length > 0) {
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        var id = checkStatus.data[i].id;
                        if (obj.data.id == id) {
                            t = true;
                            break;
                        }
                    }
                }
                if (!t) {
                    obj.update({
                        agentcount: 0
                    });
                }                    
            });   
            table.on('edit(dgrid)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
              
               obj.data.LAY_CHECKED = true;
                
               
                
            });
            table.on('rowDouble(dgrid)', function (obj) {               
            });
        });
        //平均分配？？暂时有问题，修改不了值 
        function Avg() {
            var checkStatus = layui.table.checkStatus('dgrid');
            var s =@ViewBag.TotalDis;
            var le = checkStatus.data.length;
            var Avgzs = parseInt(s / le);
            var Avgys = s % le;
            if (checkStatus.data.length > 0) {
                for (var i = 0; i < checkStatus.data.length; i++) {
                    if (i < Avgys)
                        checkStatus.data[i].agentcount = Avgzs + 1;
                    else
                        checkStatus.data[i].agentcount = Avgzs;
                    $("#Yfpsl").html(s);   
                    $("#Wfpsl").html(0);  
                    checkStatus.data[i].LAY_CHECKED=true
                }
               
            }
            
            var data1 =checkStatus.data; 
           //// var data1 = [{ 'id': 1, 'sshowname': 'dkdk', 'agentcount': 3 }, { 'id': 1, 'sshowname': 'dkdk', 'agentcount': 3 }];
            var table = layui.table;
            table.init("dgrid", {
                height: 'full-120',
                data: data1
               
            });
           
        }

        function Finish() {
            //选取 人员id及数量
            var ids = "";
            var agentcounts = "";
            var checkStatus = layui.table.checkStatus('dgrid');
            if (checkStatus.data.length > 0) {
                for (var i = 0; i < checkStatus.data.length; i++) {                   
                    ids += "," + checkStatus.data[i].id;
                    agentcounts += "," + checkStatus.data[i].agentcount;
                }
                ids = ids.substring(1);
                agentcounts = agentcounts.substring(1);
            }
            $.ajax({
                type: 'POST',
                url: "../Customer/Distribution/Finish",
                data: {
                    Ids:'@ViewBag.Ids',
                    KeyCodes:'@ViewBag.KeyCodes',
                    adviserIds: ids,
                    agentCounts: agentcounts,
                    t: new Date().getTime()
                },
                dataType: "json",
                success: function (data) {
                    if (data.code == "0000") {
                        layui.layer.msg("分配成功", {
                        }, function () {
                            parent.location.reload();
                            });
                    } else
                        layui.layer.msg(data.message);
                }
            });
        }

    </script>

</body>
</html>